<?php 
require_once('../kelas/fungsi.php');
require_once('../kelas/amankanParam.php');
require_once('../kelas/record.php');
require_once('../kelas/useradmin.php');
$dtPost = $objParamAman->postMethod();
$dtGet  =$objParamAman->getMethod();
$perpage = 3;
$hlmnn= isset($dtGet['hal'])?$dtGet['hal']:0;
$lim = $hlmnn*$perpage;
$getUser = $objUseradmin->getAllUser($perpage,$lim);
//echo $objUseradmin->strQuery;
$dtUser = isset($getUser['result'])?$getUser['result']:array();
//$objFungsi->var_dump($dtUser);
?>
<script type="text/javascript">
function editUser(idU){
	$.ajax({
		url:baseUrl+'proses/administrasi.php?on=getForEdit',
		type:'POST',
		dataType:'JSON',
		data:{
			id:idU
		},
		success:function(rslt){
			if(rslt.success==false){
				alert(rslt.error_msg);
			}
			else{
				//id,nama,alamat,jabatan,lahir,waktu,username,email,aktif
				var resultA = rslt.result[0];
				$(document).find('form[name="frmUseradmin"]').find('input[name="id"]').val(resultA.id);
				$(document).find('form[name="frmUseradmin"]').find('input[name="nama"]').val(resultA.nama);
				$(document).find('form[name="frmUseradmin"]').find('textarea[name="alamat"]').val(resultA.alamat);
				$(document).find('form[name="frmUseradmin"]').find('select[name="jabatan"]').val(resultA.jabatan);
				$(document).find('form[name="frmUseradmin"]').find('input[name="lahir"]').val(resultA.lahir);
				$(document).find('form[name="frmUseradmin"]').find('input[name="username"]').val(resultA.username);
				$(document).find('form[name="frmUseradmin"]').find('input[name="email"]').val(resultA.email);
				$(document).find('form[name="frmUseradmin"]').find('input[name="password"]').val('');
				$(document).find('form[name="frmUseradmin"]').find('input[name="aktif"]').removeAttr('checked');
				if(resultA.aktif==1){
					$(document).find('form[name="frmUseradmin"]').find('input[name="aktif"]').click();
				}

			}
		}
	});
	return false;
};
</script>
<link rel="stylesheet" type="text/css" href="<?php echo BASEURL; ?>">
<?php
echo $objFungsi->buatJudul('User');
?>
<form name="frmUseradmin" method="POST" action="#">
	<input type="hidden" name="id">
	<table>
		<tr>
			<td>Nama</td>
			<td><input name="nama" 
				class="text_box_3" 
				placeholder="Nama" 
				required="required" /></td>
			</tr>
			<tr>
				<td>Alamat</td>
				<td><textarea 
					name="alamat" 
					class="text_box_3" 
					placeholder="Alamat" 
					></textarea></td>
				</tr>
				<tr>
					<td>Jabatan</td>
					<td><select name="jabatan" 
						class="text_box_3" 
						placeholder="Jabatan" 
						required="required" >
						<option></option>
						<option value="1">Super Administrasi</option>
						<option value="2">Administrasi Pesanan</option>				
					</select></td>
				</tr>
				<tr>
					<td>Tanggal Lahir</td>
					<td><input name="lahir" 
						class="text_box_3" 
						placeholder="Format (YYYY-MM-DD)"/></td>
					</tr>
					<tr>
						<td>Email</td>
						<td><input name="email" 
							class="text_box_3"
							type="email" 
							placeholder="Email" 
							required="required" /></td>
						</tr>
						<tr>
							<td>Username</td>
							<td><input name="username" 
								class="text_box_3" 
								placeholder="Username" 
								required="required" /></td>
							</tr>
							<tr>
								<td>Password</td>
								<td><input name="password"  class="text_box_3" placeholder="Password" type="password"  /></td>
							</tr>
							<tr>
								<td>Ulangi Password</td>
								<td><input name="repass" class="text_box_3" placeholder="Ulangi Password" type="password"   /></td>
							</tr>
							<tr>
								<td>Aktif</td>
								<td><input name="aktif" type="checkbox"
									checked="checked" value="1" class="text_box_3" /></td>
								</tr>
								<tr>
									<td colspan="2"><input type="submit" value="Simpan" /><input type="reset" value="Reset" /> </td>
								</tr>
							</table>

						</form>

<div class="frame_table_order">
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">

    <thead>
        <tr align="center" valign="middle" bgcolor="#CCCCCC">
        								<td>No</td>
								<td>Nama</td>
								<td>Username</td>
								<td>Email</td>
								<td>Status</td>
								<td>Jabatan</td>
								<td colspan="2">Pilihan</td>
							</tr>
</thead>
							<?php 
							if(count($dtUser)>0){
								$no =$lim+1;
								foreach ($dtUser as $dtUser): ?>			
            <tr class="font_isi_judul" align="center" valign="middle">
									<td><?php echo $no++ ?></td>
									<td><?php echo $dtUser['nama'] ?></td>
									<td><?php echo $dtUser['username'] ?></td>
									<td><?php echo $dtUser['email'] ?></td>
									<td><?php echo ($dtUser['aktif']==1)?'Aktif':'NonAktif'; ?></td>
									<td><?php echo $objUseradmin->meanJabatan($dtUser['jabatan']); ?></td>
									<td><a class="button nonAktifUser" href="<?php echo BASEURL.'proses/administrasi.php?on=activateUser&id='.$dtUser['id']."&act=".((int)$dtUser['aktif']); ?>"><?php echo ($dtUser['aktif']==1)?"NonAktifkan":"Aktifkan"; ?></a></td>
									<td><a class="button editUser" onclick="return editUser(<?php echo $dtUser['id']?>);">Edit</a></td>
								</tr>
								<?php endforeach; 
							}
							else{

								?>
            <tr class="font_isi_judul" align="center" valign="middle">
									<td colspan="5">(tidak Ada data)</td>
								</tr>
								<?php
							}?>
						</tr>
						<tr>
							<td colspan="8"><?php 
							echo  $objFungsi->buatHalaman(isset($getUser['total'])?$getUser['total']:0, $perpage, $hlmnn, '&v=master&p=muser');
							?></td>
						</tr>
					</table>
					<script type="text/javascript">
					$(document).ready(function(){
						
						frmUser = $(document).find('form[name="frmUseradmin"]');		
						frmUser.submit(function(e){

							var passWd= $(document).find('form[name="frmUseradmin"]').find('input[name="password"]').val();
							var repass= $(document).find('form[name="frmUseradmin"]').find('input[name="repass"]').val();
							var hiddenID= $(document).find('form[name="frmUseradmin"]').find('input[name="id"]').val();
							if(passWd.length==0 && hiddenID.length==0){
								alert('Silahkan Lengkapi Isian Password');
								return false;
							}
							if(repass==passWd){
								$.ajax({
									url:baseUrl+"proses/administrasi.php?on=simpan",
									data:frmUser.serialize(),
									type:'POST',
									dataType:'JSON',
									success:function(e){
										if(e.success==false){
											alert(e.error_msg);
										}
										else{
											//alert(e.sql);
											document.location = document.URL;
										}

									},
									error:function(ed){
										alert('Gagal Menyimpan User');
									}
								});
							}
							else{
								console.log(passWd);
								console.log(repass);
								alert('Ulangi Password Salah');
							}
			//alert(frmUser.attr('action'));
			e.preventDefault();
		});
					});
</script>